package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.R;
import com.hermit.qmst.mode.MessageInfo;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;
import com.hermit.qmst.tools.Preferences;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/10/5 0005.
 */
public class MessageDetailsAcitivity extends Activity implements View.OnClickListener {
    private String TAG = "MessageDetailsAcitivity";
    private TextView title;
    private WebView wv_content;
    private TextView tx_love_count, tv_praise, tv_additional;
    private TextView tv_title, tv_time;
    private CheckBox cb_love;
    private MessageInfo data;
    private Preferences preferences;
    private ProgressDialog mProgressDialog;
    private boolean isPraise = false;
    private int message_data_position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_detail);
        mProgressDialog = ProgressDialog.show(this, "提示", "系统正在处理，请稍后…", false, false);
        preferences = new Preferences(MessageDetailsAcitivity.this);
        initView();
        message_data_position = getIntent().getIntExtra("message_data_position", 1);
        data = (MessageInfo) getIntent().getSerializableExtra("message_data");
        if (data != null) {
            initData();
        }
        title = (TextView) findViewById(R.id.tv_title);
        title.setText("消息");

    }

    private void initView() {
        ((ImageView) findViewById(R.id.iv_title_left)).setImageResource(R.drawable.ic_back);
        findViewById(R.id.iv_title_left).setOnClickListener(this);
        wv_content = (WebView) findViewById(R.id.wv_content);
        tx_love_count = (TextView) findViewById(R.id.tx_love_count);
        tv_praise = (TextView) findViewById(R.id.tv_praise);
        tv_title = (TextView) findViewById(R.id.tv_title1);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_additional = (TextView) findViewById(R.id.tv_additional);
        cb_love = (CheckBox) findViewById(R.id.cb_love);
        cb_love.setOnClickListener(this);
    }

    private void initData() {
        getData(1);
        tv_time.setText(data.getCreatetime());
        tv_title.setText(data.getTitle());
        tx_love_count.setText(data.getRead());

        WebSettings webSettings = wv_content.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        //设置支持缩放
        wv_content.setWebViewClient(new WebViewClient());
//        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
//        webSettings.setUseWideViewPort(true);
        String encoding = "UTF-8";
        String mimeType = "text/html";
        String html = Html.fromHtml(data.getContent()).toString();
        String htmlcontent="";
        if (html.indexOf("<html>") < 0) {
            htmlcontent = "<html><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>" +
                    "</title><style>img{max-width:100%;}video{max-width:100%;}</style></head><body>" + html +
                    "</body></html>";
        } else {
            htmlcontent = html;
        }
        Log.i(TAG, html, true);
        wv_content.loadDataWithBaseURL(null, htmlcontent, mimeType, encoding, null);
    }

    private void getData(final int type) {
        String url = Common.submitReaderMessage() + "u_id=" + Common.mUserId + "&m_id=" + data.getId() + "&type=" + type;
        Log.i(TAG, url, true);
        RequestQueue requestQueue = Volley.newRequestQueue(MessageDetailsAcitivity.this);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                Log.i(TAG, object.toString(), true);
                try {
                    boolean state = object.getBoolean("state");
                    if (state) {
                        tx_love_count.setText("阅读量：" + object.getString("readnum"));
                        tv_praise.setText(object.getString("praise") + "已赞");
                        if (type == 2) {
                            isPraise = true;
                            cb_love.setBackgroundResource(R.drawable.tab_bar_zan_current);
                            tv_additional.setText(object.getString("additional"));
                        }
                        if (object.getInt("type") == 2) {
                            tv_additional.setText(object.getString("additional"));
                            cb_love.setBackgroundResource(R.drawable.tab_bar_zan_current);
                            isPraise = true;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    mProgressDialog.dismiss();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mProgressDialog.dismiss();
            }
        });
        requestQueue.add(request);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.cb_love:
                if (!isPraise) {
                    mProgressDialog = ProgressDialog.show(this, "提示", "系统正在处理，请稍后…", false, false);
                    getData(2);
                } else {
                    Toast.makeText(MessageDetailsAcitivity.this, "您已经赞过了！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
